<template>
  <div class="opt_container">
    <div class="tip">
      <template v-if="userStore.isLoginState">
        <a-dropdown>
          <div class="user">
            <span>
              <img class="avatar" :src="userInfoStore.userInfo.avatar">
            </span>
            <span class="user_name">{{ userInfoStore.userInfo.name }}</span>
          </div>
          <template #overlay>
            <a-menu class="overlay">
              <a-menu-item @click="handleGo('/user/publish')">
                商品管理
              </a-menu-item>
              <a-menu-item @click="handleGo('/user/shop')">
                购买订单
              </a-menu-item>
              <a-menu-item @click="handleGo('/user/order')">
                售卖订单
              </a-menu-item>
              <a-menu-item v-if="userInfoStore.userInfo.userRole == 'admin'" @click="handleGo('/manage')">
                super admin
              </a-menu-item>
              <a-menu-item @click="handleGo('/user/info')">
                个人主页
              </a-menu-item>
              <a-menu-item @click="logout">
                退出登录
              </a-menu-item>
            </a-menu>
          </template>
        </a-dropdown>
      </template>
      <template v-else>
        <div class="login" @click="openLogin">登录</div>
      </template>

    </div>


  </div>
</template>

<script setup lang="ts">
import { useLoginStore } from '@/stores/module/login';
import { useUserStore } from '@/stores/module/user';
import storage from '@/storage';
import { wsLogin, wsLogout } from '@/utils/ws/wsLogin';
import { message } from 'ant-design-vue';
import { useRouter } from 'vue-router';
import { useUserInfoStore } from '@/stores/module/user/userInfo';
import { onMounted } from 'vue';

onMounted(() => {
  if (userStore.isLoginState) {
    userInfoStore.getUserInfo()
  }
})
const userInfoStore = useUserInfoStore()
const router = useRouter()
const loginStore = useLoginStore()
const userStore = useUserStore()
const openLogin = () => {
  wsLogin()
  loginStore.setOpenLoginToTrue()
}
const handleGo = (path) => {
  router.push(path)
}
const logout = () => {
  storage.remove('token')
  storage.remove('userStore')
  storage.remove('useLogin')
  loginStore.$reset()
  userStore.$reset()
  wsLogout()
  message.success('退出登录')
  router.push('/main')
}
</script>

<style lang="less" scoped>
.opt_container {
  width: 100%;
  height: 100%;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 14px;
  color: #665d5d;

  .tip {
    padding: 3px;
    text-align: center;
    line-height: 24px;
    width: 138px;
    height: 24px;
    cursor: pointer;

    .user {
      display: flex;
      justify-content: center;
      align-items: center;

      .avatar {
        width: 24px;
        height: 24px;
        border-radius: 50%;
      }

      .user_name {
        width: 44px;
        margin-left: 12px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
      }
    }
  }

  .overlay {
    .num {
      margin-left: 10px;
    }
  }
}
</style>
